package com.vhall.component.framework.common.enums;

import java.nio.charset.Charset;

/**
 * <p>
 * 功能: http request content type
 * </p>
 *
 * @author xiaoliang.wang@vhall.com
 * @date 2021年5月7日
 */
public enum HttpContentType {

    /**
     * json
     */
    APPLICATION_JSON("application/json", Consts.UTF_8, false),
    /**
     * xml
     */
    X_REQUESTED_WITH("xmlhttprequest", Consts.UTF_8, false),
    /**
     * form
     */
    APPLICATION_FORM_URLENCODED("application/x-www-form-urlencoded", Consts.UTF_8, false),
    /**
     * form-data
     */
    MULTIPART_FORM_DATA("multipart/form-data", Consts.UTF_8, false),
    /**
     * atom+xml
     */
    APPLICATION_ATOM_XML("application/atom+xml", Consts.UTF_8, true),
    /**
     * octet-stream
     */
    APPLICATION_OCTET_STREAM("application/octet-stream", (Charset) null, true),
    /**
     * svg+xml
     */
    APPLICATION_SVG_XML("application/svg+xml", Consts.UTF_8, true),
    /**
     * xhtml+html
     */
    APPLICATION_XHTML_XML("application/xhtml+xml", Consts.UTF_8, true),
    /**
     * xml
     */
    APPLICATION_XML("application/xml", Consts.UTF_8, true),
    /**
     * text/html
     */
    TEXT_HTML("text/html", Consts.UTF_8, false),
    /**
     * text/plain
     */
    TEXT_PLAIN("text/plain", Consts.UTF_8, false),
    /**
     * text/xml
     */
    TEXT_XML("text/xml", Consts.UTF_8, false),
    /**
     * no stream
     */
    NO_STREAM("nostream/nostream", (Charset) null, false);

    public static final String CONTENT_TYPE = "Content-Type";

    private String contentType;

    private Charset charset;

    private boolean attachment;

    private HttpContentType(String contentType, Charset charset, boolean attachment) {
        this.contentType = contentType;
        this.charset = charset;
        this.attachment = attachment;
    }

    private HttpContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getContentType() {
        return contentType;
    }

    public Charset getCharset() {
        return charset;
    }

    public boolean isAttachment() {
        return attachment;
    }

    public static final HttpContentType parse(String contentType) {
        if (contentType != null &&
                (contentType = contentType.trim().toLowerCase()).length() != 0) {
            for (HttpContentType type : HttpContentType.values()) {
                if (contentType.startsWith(type.contentType)) {
                    return type;
                }
            }
        }
        return NO_STREAM;
    }

    @Override
    public String toString() {
        return contentType + ";" + (charset == null ? "" : "charset=" + charset.toString());
    }

}